package enchere.ihm.vue;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import enchere.Article;
import enchere.EnchereRemote;
import enchere.Personne;

public class ArticleControlleur {

	private ArticleCreation articleCreation;
	private EnchereRemote enchereRemote;
	private Personne personneConnecte;

	public ArticleControlleur(ArticleCreation articleCreation) throws NamingException {
		this.articleCreation = articleCreation;
		Context ctx = new InitialContext();
		enchereRemote = (EnchereRemote) ctx.lookup(EnchereRemote.class.getName());
	}
	
	public void creerArticle(String nom, String prix){
		Article article = new Article();
		article.setVendeur(personneConnecte);
		article.setDescription(nom);
		try{
			article.setPrixInitial(Double.parseDouble(prix));
		}catch(Exception e){
			articleCreation.setError("Prix invalide");
			return;
		}
		article.setVendu(false);
		enchereRemote.proposerArticle(article);
		articleCreation.setVisible(false);
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MenuEncheres().setVisible(true);
	        }
	    });
	}

}
